local SkillExcel    = require("excel.Skill").Skill
local Timer       = require("core.Timer")
local Obj       = require("core.Obj")
local Msg       = require("core.Msg")
local Status      = require("scene.Status")
local MonsterAi     = require("monster.MonsterAi")
local SkillDefine   = require("skill.Define")
local Skill       = require("skill.Skill")
local Notify      = require("skill.Notify")
local SkillKill     = require("skill.Kill")
local BufferCmds  = require("buffer.Cmds")
local Buffer      = require("buffer.Buffer")
local PetAi = require("pet.PetAi")
local ShenbingJueji = require("shenbing.Jueji")

--local EQUIP_CALL_SKILL_CONDITION_HIT  = SkillDefine.EQUIP_CALL_SKILL_CONDITION_HIT
--local EQUIP_CALL_SKILL_CONDITION_BE_HIT = SkillDefine.EQUIP_CALL_SKILL_CONDITION_BE_HIT
local CALL_TYPE_EQUIP         = SkillDefine.CALL_TYPE_EQUIP -- 装备调用技能
--local equipCallSkill          = EquipCallSkill.callSkill
--[[
local baojiCallSkillBuffer        = BufferCmds.passive and BufferCmds.passive.baojiCallSkill
local shanbiCallSkillBuffer       = BufferCmds.passive and BufferCmds.passive.shanbiCallSkill
local gedangCallSkillBuffer       = BufferCmds.passive and BufferCmds.passive.gedangCallSkill
local beHitCallSkillBuffer        = BufferCmds.passive and BufferCmds.passive.beHitCallSkill
local hitCallSkillBuffer        = BufferCmds.passive and BufferCmds.passive.hitCallSkill
--]]

local onKillSkillKill         = SkillKill.onKill
local now
function onUse(obj, skillID, skillConfig, x, y, target)
    now = Timer.now

    Obj.onSkill(obj, x, y,skillID)

    local onUseSkillLastTime = obj.onUseSkillLastTime
    if (obj.obj_type == Obj.TYPE_HUMAN or target and target.obj_type == Obj.TYPE_HUMAN) and (not onUseSkillLastTime or now - onUseSkillLastTime >500) then
        obj.onUseSkillLastTime = now
        local s_obj_type = obj.obj_type 
        local obj, target = Obj.getOwner(obj) or obj, Obj.getOwner(target) or target
        MonsterAi.onOwnerAttack(obj, target)
        
        if s_obj_type ~= Obj.TYPE_PET then
          PetAi.onOwnerAttack(obj, target)
        end

    end

    local userData      = Skill.getUserData(obj)
    local lastSkillID   = userData.lastSkillID
    local lastSkillConfig   = SkillExcel[lastSkillID]

    if lastSkillID~=skillID and lastSkillConfig and lastSkillConfig.isLoop>0 and skillConfig.callType~=CALL_TYPE_EQUIP then
        --    onSkillStopBuffer(obj)  -- 使用技能删除BUFFER
    end

    Status.onSkill(obj)
end
--根据攻击和被攻击的对象类型来计算百分比
function calcShanghaiByObj(obj, target)
  if obj.obj_type == Obj.TYPE_PET and target.obj_type == Obj.TYPE_MONSTER then
    return 1
  elseif obj.obj_type == Obj.TYPE_PET and target.obj_type == Obj.TYPE_HUMAN then
    return 0.5
  elseif obj.obj_type == Obj.TYPE_PET and target.obj_type == Obj.TYPE_PET then
    return 1
  elseif obj.obj_type == Obj.TYPE_MONSTER and target.obj_type == Obj.TYPE_PET then
    return 0.5
  elseif obj.obj_type == Obj.TYPE_HUMAN and target.obj_type == Obj.TYPE_PET then
    return 0.7
  else
    return 1
  end   
end


local hitData = {}
function onAttack(obj, target, skillID, utilityConfig, isHit, isHeavyHit, isGedang, shangHai, noDie, isGiddy, isLuoma, isQuake)
    local getHp = Obj.getHp
    local currHp = getHp(target)
    if currHp<1 then return end   -- 目标已经死亡

    if BufferCmds.passive.invincible(target) or BufferCmds.passive.invincible(target) then  -- 无敌/临时无敌
        local mm = Msg.gc.GC_SKILL_TIP
        mm.type = 3
        mm.obj_id = target.id
        Msg.send(mm,target.fd)
        Msg.send(mm,obj.fd)
        return
    end
    now = Timer.now

    if isHit then
        if shangHai<1 then assert(nil, shangHai)end
        if isHeavyHit then
            onHeavyHit(obj)     -- 暴击
        end
        hitData.shangHai = shangHai
        hitData.isHeavyHit = isHeavyHit
        hitData.skillID = skillID
        onHit(obj, target, hitData) -- 击中
        shangHai = hitData.shangHai
        isHeavyHit = hitData.isHeavyHit
        if target.bufAttr.shanghaiOffset then
            shangHai = BufferCmds.passive.shanghaiOffset(obj, target, shangHai)    -- 伤害抵消
        end
        onBeHit(target, obj, currHp, shangHai, noDie)    -- 被击中
        
        Notify.GC_SKILL_HIT(target, obj.id, shangHai, isHeavyHit, utilityConfig)

        if getHp(target) <1 then  -- 重新获取一次 因为无敌原因
            onDie(target, Obj.getOwner(obj) or obj, utilityConfig, shangHai, isQuake)    -- 死亡
        else
            if isGiddy then
              onGiddy(target,obj)
            end
            if isLuoma then
              onLuoma(target, obj)
            end
        end

        if isGedang then    -- 格挡
            onGeDang(target, obj)
        end
    else
        onShanbi(target, obj) -- 闪避
    end
end

-- 击中人
function onHit(obj, target, data)
    now = Timer.now
    local owner   = Obj.getOwner(obj) or obj
    local objType = owner.obj_type
    local onHitLastTime = owner.onHitLastTime
    local shangHai = data.shangHai
    local skillID = data.skillID
    
    if skillID and objType == Obj.TYPE_HUMAN then       
        local onJuejiLastTime = owner.onJuejiLastTime
        if (not onJuejiLastTime or now - onJuejiLastTime > 20000) and SkillExcel[skillID] and SkillExcel[SkillExcel[skillID].group] and SkillExcel[SkillExcel[skillID].group].sb then --神兵绝技
             owner.onJuejiLastTime = now
             ShenbingJueji.onHit(owner, target, SkillExcel[SkillExcel[skillID].group].sb,data)            
        end
    end
    
    shangHai = data.shangHai
    local oldHeavyHit = data.isHeavyHit
    if not onHitLastTime or now - onHitLastTime > 500 then -- 攻击时触发 时间限制
        owner.onHitLastTime = now
        
        BufferCmds.passive.reflex(obj, target, shangHai)             -- 伤害反射
        if objType == Obj.TYPE_MONSTER then
            BufferCmds.passive.monsterReflex(obj, target, shangHai)        -- 伤害反射(怪物)
        end
        BufferCmds.passive.squelch(owner, target, shangHai)            -- 反击
        
        data.shangHai = shangHai
        data.isHeavyHit = oldHeavyHit
    end
     
    if skillID then 
        Status.onHit(obj)
    end
            
    if obj.obj_type == Obj.TYPE_HUMAN then
        obj.attacker_id = target.id
        obj.attacker_uid = target.uid 
        obj.attacker_time = Timer.now
    end
    return
end

-- 被击中
function onBeHit(obj, sender, currHp, shangHai, noDie)
    local owner = Obj.getOwner(sender) or sender

    local hp = currHp-shangHai
    if hp<1 and noDie then
        Obj.setHp(obj, 1)   -- 扣血 如果无敌的话 这里会回满血
    else
        Obj.setHp(obj, hp)
    end

    Obj.onBeHit(owner, obj, shangHai)

    local onBeHitLastTime = obj.onBeHitLastTime
    if not onBeHitLastTime or now - onBeHitLastTime > 500 then -- 被击中时触发 时间限制
        obj.onBeHitLastTime = now
        BufferCmds.passive.vampire(owner, shangHai)                -- 吸血

        if obj.obj_type == Obj.TYPE_HUMAN then
            --      equipCallSkill(obj, sender, EQUIP_CALL_SKILL_CONDITION_BE_HIT, sender.x, sender.y)
            -- 境界
            --      onBeHitJingjie(obj)
            -- Buffer
            --      beHitCallSkillBuffer(sender, obj)
        end
    end
    
    if obj.obj_type == Obj.TYPE_HUMAN and (not obj.attacker_time or obj.attacker_time +5000 < Timer.now) then
        obj.attacker_id = sender.id
        obj.attacker_uid = sender.uid 
        obj.attacker_time = Timer.now
    end
end

-- 暴击
function onHeavyHit(obj)
    if obj.obj_type == Obj.TYPE_HUMAN then
        local onBaojiLastTime = obj.onBaojiLastTime
        if not onBaojiLastTime or now - onBaojiLastTime > 1000 then -- 暴击时触发 时间限制
            obj.onBaojiLastTime = now
            -- 境界
            --      onBaojiJingjie(obj)
            -- Buffer
            --      baojiCallSkillBuffer(obj)
        end
    end
end

-- 闪避
function onShanbi(obj, sender)
    local mm = Msg.gc.GC_SKILL_TIP
    mm.type = 0
    mm.obj_id = obj.id
    Msg.send(mm,obj.fd)
    Msg.send(mm,sender.fd)
    if obj.obj_type == Obj.TYPE_HUMAN then
        local onShanbiLastTime = obj.onShanbiLastTime
        if not onShanbiLastTime or now - onShanbiLastTime > 1000 then -- 闪避时触发 时间限制
            obj.onShanbiLastTime = now
            -- 境界
            --     onShanbiJingjie(obj)
            -- Buffer
            --     shanbiCallSkillBuffer(obj)
        end
    end
end

-- 格挡
function onGeDang(obj, sender)
    local mm = Msg.gc.GC_SKILL_TIP
    mm.type = 1
    mm.obj_id = obj.id
    Msg.send(mm,sender.fd)

    if obj.obj_type == Obj.TYPE_HUMAN then
        now = Timer.now
        local onGeDangLastTime = obj.onGeDangLastTime
        if not onGeDangLastTime or now - onGeDangLastTime > 1000 then -- 格挡时触发 时间限制
            obj.onGeDangLastTime = now

            -- Buffer
            --      gedangCallSkillBuffer(obj, sender)
        end
    end
end

-- 死亡
function onDie(obj, sender, utilityConfig, shangHai,isQuake)
    sender = Obj.getOwner(sender) or sender
    Obj.onDie(obj, sender, utilityConfig, shangHai / Obj.getHpMax(obj) , isQuake)

    if sender and sender.obj_type == Obj.TYPE_HUMAN then
        now = Timer.now
        onKillSkillKill(sender) -- 击杀计数
        local onDieLastTime = sender.onDieLastTime
        if not onDieLastTime or now - onDieLastTime > 1000 then -- 杀死目标时触发 时间限制
            sender.onDieLastTime = now
        end
    end

end

--眩晕
function onGiddy(obj,sender)
    Buffer.add(201, obj,sender)
end

--落马
function onLuoma(obj,sender)
   if obj.obj_type ~= Obj.TYPE_HUMAN then return end
    
   Buffer.add(202, obj,sender)
end

